package at.guigu.web.servlet;

import at.guigu.pojo.Brand;
import at.guigu.service.BrandService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllServlet")
public class SelectAllServlet extends HttpServlet {
    //1 创建一个私有的BrandService对象
    private BrandService brandService = new BrandService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //2 调用BrandService中的方法查询所有数据
        List<Brand> brands = brandService.getAll();

        //3 将集合转为JSON字符串数据:即将对象序列化为数据
        String jsonStr = JSON.toJSONString(brands);

        //4 处理中文乱码
        response.setContentType("text/json;charset=utf-8");

        //5 响应数据
        response.getWriter().write(jsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}